#include "questioncollect.h"
#include "ui_questioncollect.h"

questioncollect::questioncollect(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::questioncollect)
{
    ui->setupUi(this);

    /* 创建选项按钮组对象 */
    m_answerButtonGroup = new QButtonGroup(this);
    /*设置按钮组排他性 */
    m_answerButtonGroup->setExclusive(true);

    m_defultStyleSheet = QString("QPushButton {"
                                "background-color: #888888;"
                                "color: white;"
                                "border-radius: 5px;"
                                "padding: 10px;"
                                "}"
                                "QPushButton:hover {"
                                "background-color: #2980b9;"  // 悬停时的背景颜色
                                "}"
                                "QPushButton:pressed {"
                                "background-color: #1abc9c;"  // 按下时的背景颜色
                                "}");
    /* 将按钮添加到按钮组中 */
    for (int idx = 1; idx <= 4; idx++)
    {
        QPushButton * button = findChild<QPushButton*>(QString("answer%1").arg(idx));
        if (button != NULL)
        {
            button->setStyleSheet(m_defultStyleSheet);
            m_answerButtonGroup->addButton(button, idx);
        }
    }

    /* 按钮组按钮个数 */
    m_buttonSize = m_answerButtonGroup->buttons().size();

    /* 选项按钮组 */
    connect(m_answerButtonGroup, QOverload<int>::of(&QButtonGroup::buttonClicked), this, &questioncollect::buttonGroupClickSlot);
}

questioncollect::~questioncollect()
{
    delete ui;
}

// 设置问题的内容
void questioncollect::setQuestionContent(QString &question)
{
    ui->label->setText(question);
}

// 设置按钮样式风格
void questioncollect::setOptionButtonStyleSheet(int idx, QString &styleSheet)
{
    m_answerButtonGroup->button(idx)->setStyleSheet(styleSheet);
}

int questioncollect::getButtonSize()
{
    return m_buttonSize;    // 按钮组的个数
}

// 按钮组按钮被按下
void questioncollect::buttonGroupClickSlot(int idx)
{
    /* 发射信号 */
    emit whichOneClick(idx);
}
